using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using MindFusion.Diagramming;
using MindFusion.Drawing;
using SolidBrush = System.Drawing.SolidBrush;

namespace ElectionAlgoritmForm.Diagramming
{
    /// <summary>
    /// Summary description for IconNode.
    /// </summary>
    public class IconNode : ShapeNode
    {
        private Color _backColor;
        public Color SelectColor = Color.Transparent;
        private Image _icon;
        private string _label;
        private readonly StringFormat _format;
        static private readonly Image DefaultIcon;
        public List<String> Comment { get; set; }

        public Color BackColor
        {
            get
            {
                return Color.FromArgb(_backColor.R, _backColor.G, _backColor.B);
            }
            set
            {
                _backColor = Color.FromArgb(value.R, value.G, value.B);
                Brush = new MindFusion.Drawing.SolidBrush(Color.Transparent);
            }
        }

        static IconNode()
        {
            DefaultIcon = new Bitmap(48, 48);

            var graphics = Graphics.FromImage(DefaultIcon);
            var font = new Font("Arial", 48, FontStyle.Bold, GraphicsUnit.Pixel);

            graphics.FillRectangle(Brushes.Transparent, 0, 0, 48, 48);
            graphics.DrawString("?", font, Brushes.Black, 0, 0);

            font.Dispose();
            graphics.Dispose();
        }

        public IconNode(Diagram diagram)
            : base(diagram)
        {
            _icon = DefaultIcon;
            _label = "";
            _format = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
            Bounds = new RectangleF(Bounds.Location, CalculateSize());
            Comment = new List<string>();

            ImageAlign = ImageAlign.Fit;
            BackColor = Color.Green;
            Brush = new MindFusion.Drawing.SolidBrush(Color.Transparent);
            Pen = new MindFusion.Drawing.Pen(Color.Black, 0.2f);
        }

        public override void Draw(IGraphics graphics, RenderOptions options)
        {
            var min = Math.Min(Bounds.Height, Bounds.Width);
            graphics.FillEllipse(new SolidBrush(BackColor), Bounds.X, Bounds.Y, min, min);
            var format = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
            graphics.DrawString(Label, Font, Brushes.Black, Bounds, format);
            var s = Comment.Aggregate("", (t, st) => t + st + Environment.NewLine);
            graphics.DrawString(s, new Font("Arial", 7, FontStyle.Italic, GraphicsUnit.Pixel), Brushes.Black, new PointF((Bounds.Right + Bounds.Left) / 2, Bounds.Bottom - 20), format);
        }

        public override void DrawShadow(IGraphics graphics, RenderOptions options)
        {
        }

        private SizeF CalculateSize()
        {
            var tempImage = new Bitmap(1, 1);
            var graphics = Graphics.FromImage(tempImage);
            var measureGraphics = new GdiGraphics(graphics) { PageUnit = Parent.MeasureUnit };

            var iconSizePixels = new Rectangle(0, 0, _icon.Width, _icon.Height);
            var imageSize = MindFusion.Utilities.DeviceToDoc(
                measureGraphics, iconSizePixels);

            measureGraphics.Dispose();
            tempImage.Dispose();

            var textSize = Parent.MeasureString(_label, Font, int.MaxValue, _format);

            return new SizeF(
                Math.Max(imageSize.Width, textSize.Width),
                imageSize.Height + textSize.Height);
        }

        protected override void UpdateCreate(PointF current)
        {
            base.UpdateCreate(current);
            Bounds = new RectangleF(current, CalculateSize());
        }

        protected override void SaveTo(System.IO.BinaryWriter writer, PersistContext ctx)
        {
            base.SaveTo(writer, ctx);
            writer.Write(_label);
            ctx.SaveImage(_icon);
        }

        protected override void LoadFrom(System.IO.BinaryReader reader, PersistContext ctx)
        {
            base.LoadFrom(reader, ctx);

            _label = reader.ReadString();
            _icon = ctx.LoadImage();
        }


        public Image Icon
        {
            get { return _icon; }
            set
            {
                _icon = value;
                Bounds = new RectangleF(Bounds.Location, CalculateSize());
            }
        }

        public string Label
        {
            get { return _label; }
            set
            {
                _label = value;
                Bounds = new RectangleF(Bounds.Location, CalculateSize());
            }
        }
    }
}